/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
// pquant.hpp - PEG color quantizer.
//
// Author: Kenneth G. Maxwell
//
// Copyright (c) 1993-2000 Swell Software 
//              All Rights Reserved.
//
// Unauthorized redistribution of this source code, in whole or part,
// without the express written permission of Swell Software
// is strictly prohibited.
//
// Notes:
//
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/

#ifdef PEG_QUANT

#ifndef _PEGQUANT_
#define _PEGQUANT_

/* Macros for converting between (r,g,b)-colors and 15-bit     */
/* colors follow.                                              */

#define RGB15(a) (PEGUSHORT) (((PEGUSHORT) a.Blue & 0xf8) << 7)|\
                 ((PEGUSHORT) (a.Green & 0xf8)<<2)|\
                 ((PEGUSHORT) a.Red>>3)

#define QUANTRED(x)     (PEGUBYTE)(((x)&31)<<3)
#define QUANTGREEN(x)   (PEGUBYTE)((((x)>>5)&255)<< 3)
#define QUANTBLUE(x)    (PEGUBYTE)((((x)>>10)&255)<< 3)

#define HSIZE      32768         /* size of image histogram    */
#define    MAX_HIST            0xFFFF
#define    MAXCOLORS            256

/*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*/
typedef  struct {       /* structure for a cube in color space */
   PEGUSHORT lower;         /* one corner's index in histogram     */
   PEGUSHORT upper;         /* another corner's index in histogram */
   PEGULONG  count;         /* cube's histogram count              */
   PEGLONG   level;         /* cube's level                        */
   PEGUBYTE  rmin,rmax;   
   PEGUBYTE  gmin,gmax;   
   PEGUBYTE  bmin,bmax;   
} cube_t;


typedef struct    {
    PEGUBYTE    Blue;  
    PEGUBYTE    Green;
    PEGUBYTE    Red;
    PEGUBYTE    Count;
} PalEntry;




/*----------------------------------------------------------------------------*/
// Pegquant- PEG Color quantizer.
/*----------------------------------------------------------------------------*/
class PegQuant
{
    public:
        PegQuant();
        ~PegQuant();

        void AddColor(PegPixel Pixel);
        PEGUBYTE *GetPalette(void)
        {
            return mPalette;
        }
        PEGUINT PalSize(void) {return (PEGUINT) mPalSize;}
        PEGUINT ReduceColors(PEGINT Start = 16, PEGINT Limit = 254, 
                             PEGINT StdStart = 0);

    protected:
//        PEGUBYTE GetNewIndex( PEGLONG index );
        void InvMap(PEGLONG cubes);
//        PEGLONG LookupPalIndex(PEGUBYTE Red, PEGUBYTE Green, PEGUBYTE Blue);
        PEGUINT MedianCut(void);
        void Shrink(cube_t * Cube);

        cube_t mCubes[MAXCOLORS];  
        PEGUINT *mpHistPtr;         
        PEGUINT *mpHist;            
        PEGUBYTE ColMap[256][4];
        PEGUBYTE mPalette[256*3];
        PEGLONG mPalSize;
        PEGLONG longdim;            
        PEGUINT mStartIndex;
        PEGUINT mEndIndex;
        PEGUINT mStdPalStart;
        PEGINT mMaxColors;
};

#endif
#endif


